# -*- coding: utf-8 -*-
"""
@Time    : 2024/7/13 13:36 
@Author  : ZhangShenao 
@File    : count_down.py
@Desc    : count down处理
"""
from threading import Thread

from bacis.decorator.perf_log_decorator import perf_log


def count_down(n: int) -> None:
    """对数字n进行倒数"""

    while n > 0:
        n -= 1


@perf_log
def count_down_serial(n: int) -> None:
    """串行的count down处理"""

    # 直接调用count_down函数
    count_down(n)


@perf_log
def count_down_multi_thread(n: int) -> None:
    """使用4个线程来处理count down处理"""

    # 开启多线程来处理
    for i in range(4):
        t = Thread(target=count_down, args=[n // 4])
        t.start()
        t.join()


if __name__ == '__main__':
    # 串行的count_down
    # [count_down_serial] costs 2225.7 ms
    # count_down_serial(100000000)

    # 多线程的count_down
    # [count_down_multi_thread] costs 2088.43 ms
    # 结果发现,采用多线程的方式并没有提升性能,有时反而执行耗时会更长
    count_down_multi_thread(100000000)
